package cn.cqie.attendance.teacher.service.impl;

import cn.cqie.attendance.common.util.LocalJwt;
import cn.cqie.attendance.common.util.PageBean;
import com.github.pagehelper.PageHelper;
import cn.cqie.attendance.teacher.dao.TeacherCourseDao;
import cn.cqie.attendance.teacher.pojo.TeacherCourseReq;
import cn.cqie.attendance.teacher.pojo.TeacherCourseRes;
import cn.cqie.attendance.teacher.pojo.TeacherStudent;
import cn.cqie.attendance.teacher.service.TeacherCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wqh
 * @date 2021/6/20 17:10
 */
@Service
public class TeacherCourseServiceImpl implements TeacherCourseService {

    @Autowired
    private TeacherCourseDao teacherCourseDao;

    @Autowired
    private HttpServletRequest request;
    //当前登录教师id
    /**
     * 1.查看当前老师的所有课程安排
     * @return
     */
    @Override
    public PageBean<TeacherCourseRes> queryCourseArrangeByTeacherId(TeacherCourseReq param) {
        int teacherId = LocalJwt.getCurrentUserId(request);
        param.setTeacherId(teacherId);
         //pageHelper插件使用
        if (param.getPageNum()==null || param.getPageSize()==null){
            param.setPageNum(1);
            param.setPageSize(10);
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(param.getPageNum());
        pageBean.setPageSize(param.getPageSize());
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());

        List<TeacherCourseRes> teacherCourseResList = teacherCourseDao.queryCourseArrangeByTeacherId(param);
        return new PageBean<>(200,"success",teacherCourseResList);
    }

    /**
     * 2.查看该老师该课程下的所有学生
     */
    @Override
    public PageBean<TeacherStudent> queryStudentInfoByCourseId(Integer courseId, Integer groupId, Integer stuMajor, Integer stuGrade, Integer stuClass, String stuName, String stuNumber, Integer pageNum, Integer pageSize) {
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (pageNum==null || pageSize==null){
            pageNum = 1;
            pageSize = 10;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        map.put("courseId",courseId);
        map.put("groupId",groupId);
        map.put("stuMajor",stuMajor);
        map.put("stuGrade",stuGrade);
        map.put("stuClass",stuClass);
        map.put("stuName",stuName);
        map.put("stuNumber",stuNumber);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<TeacherStudent> teacherStudentList = teacherCourseDao.queryStudentInfoByCourseId(map);
        int total = teacherStudentList.size();
        if (null == teacherStudentList || total == 0) {
            return new PageBean<>(500, "数据为空！");
        }else {
            return new PageBean<>(200,"Success",teacherStudentList);
        }
    }
}
